package com.zzgc.modules.controller;

import com.beust.jcommander.internal.Lists;
import com.zzgc.common.entity.*;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.modules.param.Sort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 下拉框 API
 *
 * @Author:
 * @CreateTime: 2022-12-01  14:11
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("dropdown")
@Api(tags = "下拉列表")
public class DropdownController extends BaseController {

    @GetMapping("getYears")
    @ApiOperation(value = "获取年份列表", notes = "获取年份列表")
    public Result getYears() {
        SchoolGrade schoolGrade = new SchoolGrade();
        if (super.getUserRole().equals("5")) {
            schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
        }
        List<String> list = schoolGradeService.queryByYear(schoolGrade);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(list)
                .build();
    }

    @GetMapping("getGrades")
    @ApiOperation(value = "获取年级列表", notes = "获取年级列表, 只能获取当年的年级列表")
    public Result getGrades(Long schoolId, String year) {
        SchoolGrade schoolGrade = new SchoolGrade();
        if (schoolId != null) {
            schoolGrade.setSSchoolId(schoolId);
        } else {
            schoolGrade.setSSchoolId(Long.valueOf(super.getSchoolId()));
        }
        // 查询当前年份的年级列表
        if (year != null && !"".equals(year)) {
            schoolGrade.setSYear(year);
        }else {
            schoolGrade.setSYear(getsYear() + "");
        }
        // 排序
        schoolGrade.setSorts(Arrays.asList(new Sort("s_sort")));
        schoolGrade.setSize(Integer.MAX_VALUE);
        List<SchoolGrade> schoolGrades = schoolGradeService.queryByPage(schoolGrade);
        // Map<String, String> hashMap = new HashMap<>();
        // 按list插入顺序排序
        LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();
        for (SchoolGrade grade : schoolGrades) {
            hashMap.put(grade.getId() + "", grade.getSName());
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(schoolGrades)
                .build();
    }

    @GetMapping("getClasses")
    @ApiOperation(value = "获取班级列表", notes = "获取班级列表")
    public Result getClasses(Long gradeId) {
        SchoolGradeClass schoolGradeClass = new SchoolGradeClass();
        schoolGradeClass.setSSchoolId(Long.valueOf(super.getSchoolId()));
        schoolGradeClass.setSGradeId(gradeId);
        // 排序
        schoolGradeClass.setSorts(Arrays.asList(new Sort("s_sort")));
        schoolGradeClass.setSize(Integer.MAX_VALUE);
        List<SchoolGradeClass> schoolGradeClasses = schoolGradeClassService.queryByPage(schoolGradeClass);
        LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();
        for (SchoolGradeClass gradeClass : schoolGradeClasses) {
            hashMap.put(gradeClass.getId() + "", gradeClass.getSName());
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(schoolGradeClasses)
                .build();
    }

    @GetMapping("getDict")
    @ApiOperation(value = "获取类型城市列表", notes = "获取地区列表")
    public Result getDict(SysDict sysDict) {
        List<SysDict> sysDicts = sysDictService.queryByPage(sysDict);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(sysDicts)
                .build();
    }

    @GetMapping("getDictItem")
    @ApiOperation(value = "获取地县级列表", notes = "获取列表")
    public Result getDictItem(String dictId) {
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setSDictId(dictId);
        List<SysDictItem> sysDictItems = sysDictItemService.queryByPage(sysDictItem);
        if ("2".equals(dictId)) { // 如果是学校类型就修改
            for (SysDictItem dictItem : sysDictItems) {
                dictItem.setSCode(dictItem.getSName());
            }
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(sysDictItems)
                .build();
    }

    @GetMapping("getSchool")
    @ApiOperation(value = "学校下拉框")
    public Result getSchool() {
        if (!super.getUserRole().equals("4")) {
            return Result.builder().code(500).msg("暂无权限").build();
        }
        School school = new School();
        if (super.getDepartId() != null){
            school.setSDepartId(Long.valueOf(super.getDepartId()));
        }else {
            return Result.builder().code(500).msg("暂无部门信息！").build();
        }
        List<School> schools = schoolService.queryByPage(school);

        LinkedHashMap<String, String> hashMap = new LinkedHashMap<>();
        for (School school1 : schools) {
            hashMap.put(school1.getId() + "", school1.getSName());
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(hashMap)
                .build();
    }
}
